package com.school.service.impl;

import com.school.dao.AdminDao;
import com.school.dao.StudentDao;
import com.school.dao.TeacherDao;
import com.school.dao.TextBookDao;
import com.school.dao.impl.AdminDaoImpl;
import com.school.dao.impl.StudentDaoImpl;
import com.school.dao.impl.TeacherDaoImpl;
import com.school.dao.impl.TextBookDaoImpl;
import com.school.domain.Admin;
import com.school.domain.Student;
import com.school.domain.Teacher;
import com.school.domain.TextBook;
import com.school.service.AdminService;

import java.util.List;

public class AdminServiceImpl implements AdminService {
    private final AdminDao adminDao = new AdminDaoImpl();
    private final StudentDao studentDao = new StudentDaoImpl();
    private final TeacherDao teacherDao = new TeacherDaoImpl();
    private final TextBookDao textBookDao = new TextBookDaoImpl();

    @Override
    public boolean adminLogin(long aid, String pwd) {
        Admin admin = adminDao.findById(aid);
        return admin != null && admin.getPwd().equals(pwd);
    }

    @Override
    public Admin findById(long aid) {
        return adminDao.findById(aid);
    }

    @Override
    public boolean update(Admin admin) {
        return adminDao.update(admin);
    }

    @Override
    public boolean addStudent(Student student) {
        return studentDao.add(student);
    }

    @Override
    public boolean addTeacher(Teacher teacher) {
        return teacherDao.add(teacher);
    }

    @Override
    public boolean addTextBook(TextBook textBook) {
        return textBookDao.addTextBook(textBook);
    }

    @Override
    public boolean updateStudentPwd(String pwd) {
        List<Student> students = studentDao.findAll();
        for (Student student : students) {
            student.setPwd(pwd);
            System.out.println("studentDao.update(student) = " + studentDao.update(student));
        }
        return true;
    }

    @Override
    public boolean updateStudent(Student student) {
        return studentDao.update(student);
    }

    @Override
    public boolean updateTeacher(Teacher teacher) {
        return teacherDao.update(teacher);
    }

    @Override
    public Student findStudent(long sid) {
        return studentDao.findById(sid);
    }

    @Override
    public Teacher findTeacher(long tid) {
        return teacherDao.findById(tid);
    }

    @Override
    public List<Student> findAllStudent() {
        return studentDao.findAll();
    }

    @Override
    public List<Teacher> findAllTeacher() {
        return teacherDao.findAll();
    }
}
